<?php

class bobStatusLabel extends GtkLabel {
	public function __construct() {
		parent::__construct();
		$this->set_padding(0,3);
		return;
	}
}

class bobRecentFileMenuItem extends GtkMenuItem {

	public $label = null;
	public $filename;
	
	public function __construct() {
		parent::__construct();
		
		$this->label = new GtkLabel;
		$this->label->set_alignment(0,0.5);
		$this->label->set_ellipsize(Pango::ELLIPSIZE_START);
		$this->label->set_width_chars(32);
		
		$this->label->show();
		
		$this->add($this->label);
		return;
	}
	
	public function set_label($filename) {
		$this->label->set_text(($this->filename = $filename));		
		bob::runUI();		
		return;
	}
	
}

class bobRecentFileMenuItemList extends GtkEventBox {

	public $list = array();

	public function __construct($parent) {
		parent::__construct();

		$this->obj = $parent;

		$first = true;	
		for($a = 0; $a < 10; $a++) {
			$this->list[$a] = new bobRecentFileMenuItem;
			
			if($first) {
				$this->signalRoot = $this->list[$a]->connect_simple(
					'map',
					array('bob','digRootUp'),
					$parent,
					array($this,'selfConfigure')
				); $first = false;
			}
		}

		return;
	}

	public function selfConfigure() {

		$this->root = $this->obj->root;

		foreach($this->list as $iter => $item) {
			$item->connect_simple(
				'enter-notify-event',
				array($this,'updateStatusBar'),
				&$item->filename
			);
			
			$item->connect_simple(
				'activate',
				array($this,'openRecentFile'),
				&$item->filename
			);
		}
		
		return;
	}

	public function update() {
		$iter = 0;
		foreach(bob::$histFile as $file) {
			if(file_exists($file)) {
				$this->list[$iter]->set_label($file);
				$this->list[$iter]->show();
				++$iter;
			}
		} return;
	}
	
	public function updateStatusBar(&$filename) {
		$this->root->statusbar->message('main',"Open File: {$filename}");
		return;
	}
	
	public function openRecentFile(&$filename) {
		$this->root->openFile($filename);
		return;
	}

}

class bobLineEndingMenuItem extends GtkMenuItem {

	public $menu;
	public $item = array();
	public $root,$obj;
	public $signalRoot;

	public function __construct($parent) {
		parent::__construct('Line Endings');
		
		$this->obj = $parent;
		
		$this->menu = new GtkMenu;
		$this->item['LF'] = new GtkRadioMenuItem(null,'LF (\n)');
		$this->item['CRLF'] = new GtkRadioMenuItem($this->item['LF'],'CRLF (\r\n)');
		$this->item['CR'] = new GtkRadioMenuItem($this->item['LF'],'CR (\r)');
		
		$this->item[bob::$config->get('end-of-line')]->set_active(true);
		
		foreach($this->item as $eol => $item) {
			$item->connect_simple('toggled',array($this,'on_toggle'),$eol);
			$this->menu->append($item);
		}
		
		$this->set_submenu($this->menu);
		$this->show_all();
		return;
	}
	
	public function on_toggle($eol) {
	
		if(!$this->item[$eol]->get_active())
			return;
		
		$this->obj->root->on_eol_change($eol);
		
		return;
	}

}

class bobLineEndingDropDown extends GtkComboBox {

	public function __construct() {
		parent::__construct();
	
		$list = new GtkListStore(BOB_TYPE_STRING);

		$this->pack_start($render = new GtkCellRendererText);
		$this->set_attributes($render,'text',0);
		$this->set_model($list);
		
		$list->append(array('LF'));
		$list->append(array('CRLF'));
		$list->append(array('CR'));
		
		$this->set(bob::$config->get('end-of-line'));
		
		return;
	}
	
	public function set($eol) {
		
		$iter = $this->get_model()->get_iter_first();
		while($iter) {
			if($this->get_model()->get_value($iter,0) == $eol) {
				$this->set_active_iter($iter);
				break;
			}
			
			$iter = $this->get_model()->iter_next($iter);
		}
		
		return;
	}

}

?>